package com.restlet.spike.server;

import org.restlet.*;
import org.restlet.data.ChallengeScheme;
import com.restlet.spike.resource.AccountResource;
import com.restlet.spike.resource.OrderResource;
import com.restlet.spike.filter.ExtraHttpMethodOverrideFilter;

class FirstApplication extends Application {
    public static final String ROOT_URI = "file:///d:/workspace/restlet/docs/api";

    public FirstApplication(Component component) {
        super(component.getContext());
    }

    /**
     * Creates a root Restlet that will receive all incoming calls.
     */
    @Override
    public Restlet createRoot() {
        // Create a root router
        Router router = new Router(getContext());

        // Attach a guard to secure access to the directory
        Guard guard = new Guard(getContext(),
                ChallengeScheme.HTTP_BASIC, "Restlet tutorial");
        guard.getSecrets().put("test", "test".toCharArray());
        router.attach("/docs/", guard);

        // Create a directory able to expose a hierarchy of files
        Directory directory = new Directory(getContext(), ROOT_URI);
        guard.setNext(directory);

        router.attach("/users/{user}", AccountResource.class);
        router.attach("/users/{user}/orders/{order}", OrderResource.class);

        ExtraHttpMethodOverrideFilter extraHttpMethodOverrideFilter = new ExtraHttpMethodOverrideFilter();
        extraHttpMethodOverrideFilter.setNext(router);

        // Return the root router
        return extraHttpMethodOverrideFilter;
    }

}